//
//  DatePickerView.swift
//  globalcarbusiness
//
//  Created by dongzhenyu on 2017/8/22.
//  Copyright © 2017年 dongzhenyu. All rights reserved.
//

import UIKit



class DatePickerView: UIView {

    enum SelectorFormatter {
        case Year
        case Month
        case Day
    }
    var cover: UIView!
    var bgView: UIView!
    var picker: UIDatePicker!
    var selectorType: SelectorFormatter!
    var handleDateStr: ((String)->Void)?
    
    func showSelector(formatter: SelectorFormatter) {
        self.selectorType = formatter
        let cover = UIView(frame: CGRect(x: 0, y: 0, width: kScreenW, height: kScreenH))
        self.cover = cover
        cover.alpha = 0
        cover.backgroundColor = UIColor.black
        UIApplication.shared.keyWindow?.addSubview(cover)
        UIView.animate(withDuration: 0.25) { 
            cover.alpha = 0.3
        }
        
        let bgView = UIView()
        self.bgView = bgView
        bgView.center = CGPoint(x: kScreenW/2, y: kScreenH/2)
        bgView.bounds = CGRect(x: 0, y: 0, width: kScreenW - 40*WIDTH_SCALE, height: 300*HEIGHT_SCALE)
        bgView.backgroundColor = UIColor.white
        bgView.layer.masksToBounds = true
        UIApplication.shared.keyWindow?.addSubview(bgView)
        
        let cancelBtn = UIButton(type: .system)
        cancelBtn.setImage(#imageLiteral(resourceName: "icon_quxiao"), for: .normal)
        cancelBtn.frame = CGRect(x: 0, y: 0, width: 100*WIDTH_SCALE, height: 40*HEIGHT_SCALE)
        bgView.addSubview(cancelBtn)
        cancelBtn.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside)
        
        let quedingBtn = UIButton(type: .system)
        quedingBtn.setImage(UIImage(named:"icon_queding"), for: .normal)
        quedingBtn.frame = CGRect(x: kScreenW - 140*WIDTH_SCALE, y: 0, width: 100*WIDTH_SCALE, height: 40*HEIGHT_SCALE)
        bgView.addSubview(quedingBtn)
        quedingBtn.addTarget(self, action: #selector(quedingBtnClick), for: .touchUpInside)
        
        let textLabel = UILabel()
        textLabel.font = UIFont.systemFont(ofSize: 14*WIDTH_SCALE)
        bgView.addSubview(textLabel)
        textLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(40*HEIGHT_SCALE)
        }
        
        let picker = UIDatePicker.init(frame: CGRect.init(x: 10, y: 80*HEIGHT_SCALE, width: kScreenW-60*WIDTH_SCALE, height: 200*HEIGHT_SCALE))
        self.picker = picker
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let startDateStr = "2016-01-01"
        picker.minimumDate = dateFormatter.date(from: startDateStr)
        
        dateFormatter.dateFormat = "yyyy"
        var endDateStr = dateFormatter.string(from: Date())
        endDateStr.append("-12-31")
        dateFormatter.dateFormat = "yyyy-MM-dd"
        
        picker.maximumDate = dateFormatter.date(from: endDateStr)
        picker.backgroundColor = RGBA(245, g: 245, b: 245, a: 1)
        picker.addTarget(self, action: #selector(didSelectedDate(picker:)), for: UIControlEvents.valueChanged)
        picker.datePickerMode = UIDatePickerMode.date
        bgView.addSubview(picker)
        
        switch formatter {
        case .Year:
            picker.frame = CGRect.init(x: 110*WIDTH_SCALE, y: 80*HEIGHT_SCALE, width: kScreenW-60*WIDTH_SCALE, height: 200*HEIGHT_SCALE)
            
            let cview = UIView.init(frame: CGRect.init(x: quedingBtn.frame.origin.x, y: 80*HEIGHT_SCALE, width: 110*WIDTH_SCALE, height: 200*HEIGHT_SCALE))
            cview.backgroundColor = UIColor.white
            bgView.addSubview(cview)
            if kScreenW == 414.0 {
                
                picker.frame = CGRect.init(x: 80*WIDTH_SCALE, y: 80*HEIGHT_SCALE, width: kScreenW-60*WIDTH_SCALE, height: 200*HEIGHT_SCALE)
                cview.frame = CGRect.init(x: quedingBtn.frame.origin.x-20, y: 80*HEIGHT_SCALE, width: 110*WIDTH_SCALE, height: 200*HEIGHT_SCALE)
                let cview2 = UIView.init(frame: CGRect.init(x: 0, y: 80*HEIGHT_SCALE, width: cancelBtn.bounds.size.width+30, height: 200*HEIGHT_SCALE))
                cview2.backgroundColor = UIColor.white
                bgView.addSubview(cview2)
            }
        case .Month:
            picker.frame = CGRect.init(x: 70*WIDTH_SCALE, y: 80*HEIGHT_SCALE, width: kScreenW-70*WIDTH_SCALE, height: 200*HEIGHT_SCALE)
            
            let cview = UIView.init(frame: CGRect.init(x: quedingBtn.frame.origin.x+30*WIDTH_SCALE, y: 80*HEIGHT_SCALE, width: 70*WIDTH_SCALE, height: 200*HEIGHT_SCALE))
            cview.backgroundColor = UIColor.white
            bgView.addSubview(cview)
            
        case .Day:break
    
        }
    }
}

extension DatePickerView {
    
    @objc fileprivate func cancelBtnClick(sender: UIButton) {
        
        UIView.animate(withDuration: 0.25, animations: { 
            self.cover.alpha = 0
            self.bgView.alpha = 0
            
        }) { (complected) in
            self.cover.removeFromSuperview()
            self.bgView.removeFromSuperview()
        }
    }
    
    @objc fileprivate func quedingBtnClick(sender: UIButton) {
        
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        
        if selectorType == .Year {
            formatter.dateFormat = "yyyy"
        } else if selectorType == .Month {
            formatter.dateFormat = "yyyy-MM"
        }
        handleDateStr?(formatter.string(from: self.picker.date))
        cancelBtnClick(sender: sender)
    }
    
    @objc fileprivate func didSelectedDate(picker: UIDatePicker) {
        DZYLog(picker.date)
    }
}
